import copy

from PyQt5.QtWidgets import QLabel, QPushButton, QVBoxLayout, QWidget, QHBoxLayout

from utils.delLayout import delLayout


class TopBarParams:
    def __init__(self, newListener, settingListener, aboutListener, resetListener, runListener):
        self.newListener = newListener
        self.settingListener = settingListener
        self.aboutListener = aboutListener
        self.resetListener = resetListener
        self.runListener = runListener


class UI_Main:
    def __init__(self):
        self.output1 = None
        self.output2 = None
        self.init_list_show = None
        self.first_list_show = None
        self.second_list_show = None
        self.third_list_show = None
        self.time_show = None

    def refreshTime(self, time):
        self.time_show.setText("时间：{}".format(time))

    def resetInitList(self, list_input):
        list_input = copy.deepcopy(list_input)
        delLayout(self.init_list_show)
        print("init")
        for item in list_input:
            print(item.name, item.receive_time, item.need_time, item.rest_time)
            obj = QPushButton("{}".format(item.name))
            self.init_list_show.addWidget(obj)
        self.init_list_show.addStretch()

    def resetFirstList(self, list_input):
        list_input = copy.deepcopy(list_input)
        delLayout(self.first_list_show)
        print("first")
        for item in list_input:
            print(item.name, item.receive_time, item.need_time, item.rest_time)
            obj = QPushButton("{}".format(item.name))
            self.first_list_show.addWidget(obj)
        self.first_list_show.addStretch()

    def resetSecondList(self, list_input):
        list_input = copy.deepcopy(list_input)
        delLayout(self.second_list_show)
        print("second")
        for item in list_input:
            print(item.name, item.receive_time, item.need_time, item.rest_time)
            obj = QPushButton("{}".format(item.name))
            self.second_list_show.addWidget(obj)
        self.second_list_show.addStretch()

    def resetThirdList(self, list_input):
        list_input = copy.deepcopy(list_input)
        delLayout(self.third_list_show)
        print("third")
        for item in list_input:
            print(item.name, item.receive_time, item.need_time, item.rest_time)
            obj = QPushButton("{}".format(item.name))
            self.third_list_show.addWidget(obj)
        self.third_list_show.addStretch()

    def flushOutput1(self, str_input):
        obj = QLabel(str_input)
        delLayout(self.output1)
        self.output1.addWidget(obj)
        self.output1.addStretch()

    def refreshInit(self, list_input):
        list_input = copy.deepcopy(list_input)
        delLayout(self.output2)
        for item in list_input:
            obj = QPushButton("pid:{}   arrival:{}   server:{}".format(item.name, item.receive_time, item.need_time))
            self.output2.addWidget(obj)
        self.output2.addStretch()

    def createTopBar(self, top_bar_Params):
        container_widget = QWidget()
        container_widget.setStyleSheet("background-color: #002884;")

        top_bar = QHBoxLayout()

        newButton = QPushButton("新建")
        newButton.setStyleSheet("background-color: #3f50b5;")
        settingButton = QPushButton("设置")
        settingButton.setStyleSheet("background-color: #3f50b5;")
        aboutButton = QPushButton("关于")
        aboutButton.setStyleSheet("background-color: #3f50b5;")
        resetButton = QPushButton("重置")
        resetButton.setStyleSheet("background-color: #3f50b5;")

        runButton = QPushButton("运行")
        runButton.setStyleSheet("background-color: #Ff50bF;")

        timeLabel = QLabel("时间: 0")
        self.time_show = timeLabel

        newButton.clicked.connect(lambda: top_bar_Params.newListener(self))
        settingButton.clicked.connect(top_bar_Params.settingListener)
        aboutButton.clicked.connect(top_bar_Params.aboutListener)
        resetButton.clicked.connect(lambda: top_bar_Params.resetListener(self))
        runButton.clicked.connect(
            lambda: top_bar_Params.runListener(self))

        top_bar.addWidget(newButton)
        top_bar.addWidget(settingButton)
        top_bar.addWidget(aboutButton)
        top_bar.addWidget(resetButton)
        top_bar.addSpacing(40)
        top_bar.addWidget(runButton)
        top_bar.addWidget(timeLabel)
        top_bar.addStretch()

        container_widget.setLayout(top_bar)
        return container_widget

    def CreateMainWindow(self, screen, top_bar_params):
        win = QWidget()
        win.setWindowTitle("多级反馈队列调度")
        width = 800
        height = 600
        win.setGeometry((screen.width - width) / 2, (screen.height - height) / 2, width, height)

        outline_vg = QVBoxLayout()

        top_bar = self.createTopBar(top_bar_params)
        outline_vg.addWidget(top_bar)

        chart_vg = QHBoxLayout()

        init_cg_container = QWidget()
        first_cg_container = QWidget()
        second_cg_container = QWidget()
        third_cg_container = QWidget()

        init_cg_container.setStyleSheet("""
        background-color: #002884;
        border:4px solid #757de8; 
        border-top-left-radius:10px;
        border-top-right-radius:10px;
        border-bottom-left-radius:10px;
        border-bottom-right-radius:10px;
        padding:2px 4px;
        """)
        first_cg_container.setStyleSheet("""
        background-color: #002884;
        border:4px solid #757de8; 
        border-top-left-radius:10px;
        border-top-right-radius:10px;
        border-bottom-left-radius:10px;
        border-bottom-right-radius:10px;
        padding:2px 4px;
        """)
        second_cg_container.setStyleSheet("""
        background-color: #002884;
        border:4px solid #757de8; 
        border-top-left-radius:10px;
        border-top-right-radius:10px;
        border-bottom-left-radius:10px;
        border-bottom-right-radius:10px;
        padding:2px 4px;
        """)
        third_cg_container.setStyleSheet("""
        background-color: #002884;
        border:4px solid #757de8; 
        border-top-left-radius:10px;
        border-top-right-radius:10px;
        border-bottom-left-radius:10px;
        border-bottom-right-radius:10px;
        padding:2px 4px;
        """)
        init_cg_container.setFixedHeight(300)
        first_cg_container.setFixedHeight(300)
        second_cg_container.setFixedHeight(300)
        third_cg_container.setFixedHeight(300)

        init_cg = QVBoxLayout()
        first_cg = QVBoxLayout()
        second_cg = QVBoxLayout()
        third_cg = QVBoxLayout()

        chart_vg.addLayout(init_cg)
        chart_vg.addLayout(first_cg)
        chart_vg.addLayout(second_cg)
        chart_vg.addLayout(third_cg)

        ################################
        text_queue_init = QLabel("初始队列")

        init_cg.addWidget(text_queue_init)
        init_cg.addWidget(init_cg_container)

        init_list = QVBoxLayout()
        init_cg_container.setLayout(init_list)
        self.init_list_show = init_list

        ################################
        ################################

        first_cg.addWidget(QLabel("队列1"))
        first_cg.addWidget(first_cg_container)

        first_list = QVBoxLayout()
        first_cg_container.setLayout(first_list)
        self.first_list_show = first_list

        ################################
        ################################

        second_cg.addWidget(QLabel("队列2"))
        second_cg.addWidget(second_cg_container)

        second_list = QVBoxLayout()
        second_cg_container.setLayout(second_list)
        self.second_list_show = second_list

        ################################
        ################################

        third_cg.addWidget(QLabel("队列3"))
        third_cg.addWidget(third_cg_container)

        third_list = QVBoxLayout()
        third_cg_container.setLayout(third_list)
        self.third_list_show = third_list

        ################################

        ################################
        # 底部栏
        bottom_bar = QHBoxLayout()
        output_cg = QVBoxLayout()
        extra_cg = QVBoxLayout()
        bottom_bar.addLayout(output_cg)
        bottom_bar.addLayout(extra_cg)

        ################
        output_container = QWidget()
        output_content = QVBoxLayout()

        output_container.setStyleSheet("""
        background-color: #002884;
        border:4px solid #757de8; 
        border-top-left-radius:10px;
        border-top-right-radius:10px;
        border-bottom-left-radius:10px;
        border-bottom-right-radius:10px;
        padding:2px 4px;
        """)
        output_container.setFixedHeight(200)
        output_container.setLayout(output_content)
        output_cg.addWidget(QLabel("输出1"))
        output_cg.addWidget(output_container)

        self.output1 = output_content

        ################
        extra_container = QWidget()
        extra_content = QVBoxLayout()

        extra_container.setStyleSheet("""
            background-color: #002884;
            border:4px solid #757de8; 
            border-top-left-radius:10px;
            border-top-right-radius:10px;
            border-bottom-left-radius:10px;
            border-bottom-right-radius:10px;
            padding:2px 4px;
            """)
        extra_container.setFixedHeight(200)
        extra_container.setLayout(extra_content)
        extra_cg.addWidget(QLabel("初始进程"))
        extra_cg.addWidget(extra_container)

        # extra_1 = QLabel("testtesttesttest")
        # extra_content.addWidget(extra_1)
        # extra_content.addSpacing(100)

        self.output2 = extra_content

        ################################
        # 布局组合

        outline_vg.addStretch(1)
        text_hint = QLabel("队列: ")
        outline_vg.addWidget(text_hint)

        outline_vg.addLayout(chart_vg)
        outline_vg.addStretch(30)

        outline_vg.addLayout(bottom_bar)

        win.setLayout(outline_vg)

        return win
